import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QFrame, QSplitter
from PyQt5.QtCore import Qt
from datetime import datetime

class TimestampConverter(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 创建主布局
        main_layout = QVBoxLayout()

        # 时间戳转日期时间部分
        timestamp_frame = QFrame()
        # 设置边框样式，这里设置边框为 2 像素宽的蓝色实线
        # timestamp_frame.setStyleSheet("""
        #           border: 2px solid blue;
        #           border-radius: 5px;
        #           padding: 10px;
        #       """)
        timestamp_frame.setFrameShape(QFrame.StyledPanel)
        timestamp_frame.setFrameShadow(QFrame.Raised)
        timestamp_to_datetime_layout = QHBoxLayout(timestamp_frame)

        self.timestamp_input = QLineEdit()
        self.timestamp_input.setPlaceholderText("输入时间戳（支持毫秒）")
        timestamp_to_datetime_layout.addWidget(QLabel("时间戳转日期时间:"))
        timestamp_to_datetime_layout.addWidget(self.timestamp_input)
        # 连接 textChanged 信号到转换函数
        self.timestamp_input.textChanged.connect(self.timestamp_to_datetime)
        self.timestamp_to_datetime_result = QLineEdit()
        self.timestamp_to_datetime_result.setReadOnly(True)
        timestamp_to_datetime_layout.addWidget(self.timestamp_to_datetime_result)

        # 日期时间转时间戳部分
        datetime_frame = QFrame()
        # 设置边框样式，这里设置边框为 2 像素宽的绿色实线
        # datetime_frame.setStyleSheet("""
        #           border: 2px solid green;
        #           border-radius: 5px;
        #           padding: 10px;
        #       """)
        datetime_frame.setFrameShape(QFrame.StyledPanel)
        datetime_frame.setFrameShadow(QFrame.Raised)
        datetime_to_timestamp_layout = QHBoxLayout(datetime_frame)

        self.datetime_input = QLineEdit()
        self.datetime_input.setPlaceholderText("输入日期时间 (YYYY-MM-DD HH:MM:SS)")
        datetime_to_timestamp_layout.addWidget(QLabel("日期时间转时间戳:"))
        datetime_to_timestamp_layout.addWidget(self.datetime_input)
        # 连接 textChanged 信号到转换函数
        self.datetime_input.textChanged.connect(self.datetime_to_timestamp)
        self.datetime_to_timestamp_result = QLineEdit()
        self.datetime_to_timestamp_result.setReadOnly(True)
        datetime_to_timestamp_layout.addWidget(self.datetime_to_timestamp_result)

        # 创建水平分割器
        splitter = QSplitter(Qt.Vertical)
        splitter.addWidget(timestamp_frame)
        splitter.addWidget(datetime_frame)

        # 将分割器添加到主布局
        main_layout.addWidget(splitter)

        # 设置布局
        self.setLayout(main_layout)

        # 设置窗口属性
        self.setWindowTitle('时间戳格式工具')
        self.setGeometry(300, 300, 800, 150)
        self.show()

    def timestamp_to_datetime(self, text):
        timestamp_str = text.strip()
        if not timestamp_str:
            self.timestamp_to_datetime_result.clear()
            return
        try:
            timestamp = float(timestamp_str)
            # 处理毫秒级时间戳
            if len(timestamp_str) > 10:
                timestamp /= 1000
            dt = datetime.fromtimestamp(timestamp)
            self.timestamp_to_datetime_result.setText(dt.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3])
        except ValueError:
            self.timestamp_to_datetime_result.clear()

    def datetime_to_timestamp(self, text):
        datetime_str = text.strip()
        if not datetime_str:
            self.datetime_to_timestamp_result.clear()
            return
        try:
            dt = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
            timestamp = dt.timestamp()
            self.datetime_to_timestamp_result.setText(str(timestamp))
        except ValueError:
            self.datetime_to_timestamp_result.clear()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    converter = TimestampConverter()
    sys.exit(app.exec_())